package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.common.fast.FastDFSClient;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmMaterialDto;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmNewsMaterial;
import com.usian.utils.threadlocal.ThreadLocalUtils;
import com.usian.wemedia.mapper.NewsMaterialMapper;
import com.usian.wemedia.mapper.WmMaterialMapper;
import com.usian.wemedia.service.WmMaterialService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@Service
public class WmMaterialServiceImpl extends BaseServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    private FastDFSClient fastDFSClient;
    @Value("${fdfs.url}")
    private String baseUrl;
    @Autowired
    private NewsMaterialMapper newsMaterialMapper;
    @Override
    @SneakyThrows
    public ResponseResult uploadPicture(MultipartFile file) {
        if (file == null)return paramError;
        Integer userId = ThreadLocalUtils.getUserId();
        if (userId == null)return needLogin;
        //将图片上传到资源服务器
        String s = fastDFSClient.uploadFile(file);
        WmMaterial material = new WmMaterial();
        material.setUserId(userId);
        material.setUrl(s);
        material.setType((short) 0);
        material.setIsCollection((short) 0);
        material.setCreatedTime(new Date());
        //将资源服务器返回的图片保存路劲和用户标识绑定入表
        save(material);
        return success(baseUrl+s);
    }

    @Override
    public ResponseResult findMaterialList(WmMaterialDto dto) {
        if (dto == null)return paramError;
        Integer userId = ThreadLocalUtils.getUserId();
        if (userId == null)return needLogin;
        ResponseResult pageData = getPageData(dto, Wrappers.<WmMaterial>lambdaQuery().eq(WmMaterial::getUserId, userId).eq(WmMaterial::getIsCollection, dto.getIsCollection()).orderByDesc(WmMaterial::getCreatedTime));
        List<WmMaterial> list = (List<WmMaterial>) pageData.getData();
        list.forEach(w->w.setUrl(baseUrl+w.getUrl()));
        pageData.setData(list);
        return pageData;
    }

    @Override
    public ResponseResult delMaterial(Integer materialId) {
        if (materialId == null)return paramError;
        Integer userId = ThreadLocalUtils.getUserId();
        if (userId == null)return needLogin;
        //确定图片没有被新闻引用才能删除
        Integer count = newsMaterialMapper.selectCount(Wrappers.<WmNewsMaterial>lambdaQuery().eq(WmNewsMaterial::getMaterialId, materialId));
        if (count > 0)return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH,"该素材被引用不能删除");
        //从素材库删除素材文件
        WmMaterial material = getById(materialId);
        fastDFSClient.delFile(material.getUrl());
        //从素材表删除素材数据
        removeById(materialId);
        return success;
    }
}
